import axios from '@/utils/request'
export default {
  Product: {
    form: {
      fields: [
        'name',
        {
          property: 'subTitle',
          field_options: { label: '搜索标签' },
          help: '前端按照搜索标签字眼可以进行搜索，如填写“红蜘蛛”等虫害，搜索红蜘蛛的时候会搜索到对应产品,多个标签用,隔开'
        },
        {
          property: 'regionIds',
          required: false,
          field_options: { label: '销售区域' },
          default_value: [],
          component: {
            props: ['form', 'property'],
            data() {
              return {
                regions: []
              }
            },
            created() {
              this.axios.get(`/manage/regions`).then((res) => {
                console.log('regions', res.data)
                const regions = []
                for (let i of res.data) {
                  regions.push({
                    label: i.name,
                    key: i.id
                  })
                }
                this.regions = regions
              })
            },
            render(h) {
              return (
                <el-transfer v-model={this.form[this.property]} data={this.regions} titles={['未选', '已选']}>
                </el-transfer>
              )
            }
          }
        },
        {
          property: 'category',
          field_options: { label: '类型' },
          component: {
            props: ['form', 'property'],
            data() {
              return {
                categories: []
              }
            },
            created() {
              this.axios.get(`/manage/categories`, {
                params: {
                  '@filter': 'entity.getParent() && entity.getParent().getParent()',
                  '@expands': `['entity.parent.parent']`
                }
              }).then((res) => {
                console.log(11123, res.data)
                const categories = []
                for (let i of res.data) {
                  categories.push({
                    label: `${i.parent.__metadata.parent.__toString}-${i.parent.__toString}-${i.name}`,
                    value: i.id
                  })
                }
                this.categories = categories
                console.log(categories)
              })
            },
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} filterable placeholder='类型'>{
                  this.categories.map(type => {
                    return <el-option key={type.value} label={type.label} value={type.value} />
                  })
                }</el-select>
              )
            }
          }
        },
        'spu',
        // { property: 'subTitle', field_options: { label: '副标题' } },
        { property: 'isOnSale', default_value: true },
        { property: 'cover', type: 'image' },
        { property: 'price', field_options: { label: '销售价' } },
        { property: 'marketPrice', field_options: { label: '市场价' } },
        { property: 'tags', field_options: { label: '标签' } },
        { property: 'listOrder', default_value: 255 },
        { property: 'description', field_options: { label: '产品详情' } }
      ]
    },
    list: {
      // export: {
      //   query: {
      //     '@display': `{
      //       'stock' : 'entity.getStock().getSpecification().getName()',
      //       'quantity' : 'entity.getQuantity()',
      //       'unitPrice' : 'entity.getUnitPrice()',
      //       'comment' : 'entity.getComment()',
      //       'cretedTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")'
      //     }`,
      //     limit: 10000000
      //   },
      //   label: {
      //     'stock': '产品',
      //     'quantity': '数量',
      //     'unitPrice': '单价',
      //     'comment': '原因',
      //     'cretedTime': '时间'
      //   }
      // },
      // actions: [
      //   {
      //     name: '导入',
      //     position: 'top',
      //     component:
      //       () => import('@/views/product/synchronous.vue')
      //   }
      // ],
      query: {
        '@order': 'entity.listOrder|ASC',
        // '@display': "['id','name','category','cover','isOnSale','price','marketPrice','listOrder', 'spu']"
        '@display': `{
          'id' : 'entity.getId()',
          'name': 'entity.getName()',
          'category': 'entity.getCategory()',
          'cover': 'entity.getCover()',
          'isOnSale': 'entity.getIsOnSale()',
          'specifications': 'entity.getSpecifications()',
          'listOrder': 'entity.getListOrder()',
          'spu': 'entity.getSpu()',
          'parentCategory': 'entity.getCategory().getParent().getName()',
          'grouCategory': 'entity.getCategory().getParent().getParent().getName()',
          'tags': 'entity.getTags()',
          'regionIds': 'entity.getRegionIds()'
        }`
      },
      list_filter: {
        'noneSpecification': {
          expression: 'entity.getSpecifications().count() == 0',
          label: '无规格产品',
          type: 'boolean'
        },
        'category.parent.parent.id': () => {
          return axios
            .get(`/manage/categories`, { params: { '@filter': `!entity.getParent() && (entity.getType().getSlug() == 'warehouse' || entity.getType().getSlug() == 'store' || entity.getType().getSlug() == 'service')` } })
            .then(res =>
              Object.assign(
                { __label: '销售类型' },
                ...res.data.map(v => {
                  return { [v.id]: v.name }
                })
              )
            )
        },
        'category.parent.id': () => {
          return axios
            .get(`/manage/categories`, { params: { '@filter': `entity.getParent() && !entity.getParent().getParent()  && (!entity.getType() || entity.getType().getSlug() == 'product')` } })
            .then(res =>
              Object.assign(
                { __label: '二级类型' },
                ...res.data.map(v => {
                  return { [v.id]: `${v.name}[${v.parent.__toString}]` }
                })
              )
            )
        },
        'category.id': () => {
          return axios
            .get(`/manage/categories`, { params: { '@filter': `entity.getParent() && entity.getParent().getParent()  && (!entity.getType() || entity.getType().getSlug() == 'product')`, '@expands': "['entity.parent']" } })
            .then(res =>
              Object.assign(
                { __label: '三级类型' },
                ...res.data.map(v => {
                  return { [v.id]: `${v.name}[${v.parent.__metadata.parent.__toString}-${v.parent.__toString}]` }
                })
              )
            )
        },
        'name': '名称'
      },
      list_display: [
        'id',
        { property: 'name', editable: true },
        { property: 'grouCategory', label: '销售类型' },
        { property: 'parentCategory', label: '上级分类' },
        { property: 'category', label: '类型' },
        {
          property: 'spu',
          editable: true,
          label: '产品编码'
        },
        { property: 'cover', type: 'image' },
        { property: 'isOnSale', editable: true, label: '上架' },
        {
          property: 'specifications',
          label: '规格',
          component: {
            props: ['data'],
            render(h) {
              return (
                <div>
                  {
                    this.data.map(v => {
                      return <p>{v.name}<span style='color:red'>(￥{v.price})</span></p>
                    })
                  }
                </div>
              )
            }
          }
        },
        {
          property: 'regionIds',
          label: '销售区域',
          component: {
            props: ['data'],
            data() {
              return {
                regions: []
              }
            },
            created() {
              if (this.data && this.data.length > 0) {
                this.axios.get(`/manage/regions`, {
                  property: {
                    '@filter': `entity.getId() in [${this.data.toString()}]`
                  }
                }).then((res) => {
                  console.log('regions1', res.data)
                  this.regions = res.data
                })
              }
            },
            render(h) {
              if (this.regions && this.regions.length > 0) {
                return (
                  <div>
                    {this.regions.map(v => { return (<p>{v.name}</p>) })}
                  </div>
                )
              }
            }
          }
        },
        {
          property: 'tags',
          label: '标签',
          component: {
            props: ['data'],
            render(h) {
              if (this.data && this.data.length > 0) {
                return (
                  <span>{this.data.toString()}</span>
                )
              }
            }
          }
        },
        { property: 'listOrder', editable: true }
      ]
    }
  }
}
